/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.wlhy.report.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CheckBankCardIUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.LngLonUtil;
import org.jeecg.modules.third.jg.mingna.MingnaDriverDTO;
import org.jeecg.modules.third.jg.mingna.MingnaVehicleDTO;
import org.jeecg.modules.third.jg.mingna.financial.FinanciallistDTO;
import org.jeecg.modules.third.jg.mingna.financial.MingnaFinancialDTO;
import org.jeecg.modules.third.jg.mingna.financial.ShippingNoteListDTO;
import org.jeecg.modules.third.jg.mingna.shippingnote.MingnaShippingNoteDTO;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.PaymentMeansCodeEnum;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.ReportDocTypeEnum;
import org.jeecg.modules.third.jg.report.freight.information.dto.AppInfoDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.DriverDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.VehicleDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.shippingnote.ShippingNoteDTO;
import org.jeecg.modules.third.jg.report.freight.information.model.R;
import org.jeecg.modules.third.jg.report.freight.information.service.MingnaReportPlatformService;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriverReport;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverReportService;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleReport;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleType;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleReportService;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.report.IReportService;
import org.jeecg.modules.wlhy.shipping.constant.ShippingPayStatus;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 铭钠监管系统
 */
@Service
@Slf4j
public class MingnaReportService implements IReportService {

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    @Value(value = "${jeecg.path.bank}")
    private String bankpath;

    @Resource
    private BaseReportService baseReportService;

    @Resource
    private IHyDriverService driverService;

    @Resource
    private IHyVehicleService vehicleService;

    @Resource
    private IHyDriverReportService hyDriverReportService;

    @Resource
    private IHyVehicleReportService vehicleReportService;

    @Autowired
    private MingnaReportPlatformService mingnaReportPlatformService;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyShippingNoteBillService hyShippingNoteBillService;

    @Override
    public ReportPlatformEnum getReportType() {
        return ReportPlatformEnum.REPORT_PLATFORM_MINGNA;
    }

    @Override
    public void checkReport() {
        if(!HyCfgUtil.isUsed2("mingna_report")){
            throw new JeecgBootException("铭钠上报服务暂未开启,请联系管理员。");
        }
    }

    @Override
    public Result setReportInfo(HyCompanyConfig hyCompanyConfig) {

        this.mingnaReportPlatformService.setReportInfo(hyCompanyConfig.getReportUrl(),
                hyCompanyConfig.getReportUsername(),
                hyCompanyConfig.getReportPassword(),
                hyCompanyConfig.getReportPublickey(),
                hyCompanyConfig.getReportAppid(),
                hyCompanyConfig.getReportAppkey(),
                hyCompanyConfig.getReportAppsender(),
                hyCompanyConfig.getReportApprelease());
        return Result.ok("设置成功");
    }

    @Override
    public AppInfoDTO getAppInfo() {
        return this.mingnaReportPlatformService.getAppInfo();
    }

    @Override
    public boolean report(HyShippingNote hyShippingNote) {
        return false;
    }

    @Override
    public Result reportShippingNote(HyShippingNote hyShippingNote) {
        Result result = this.checkDriverAndVehicle(hyShippingNote.getId());
        if (!result.getCode().equals(CommonConstant.SC_OK_200)) {
            return result;
        }

        return this.uploadShippingNote(hyShippingNote);
    }

    @Override
    public Result reportCapitalFlow(HyShippingNote hyShippingNote) {
        Result result = this.checkDriverAndVehicle(hyShippingNote.getId());
        if (!result.getCode().equals(CommonConstant.SC_OK_200)) {
            return result;
        }

        return this.uploadCapitalFlow(hyShippingNote.getId());
    }

    //上报运单和资金流水前，先确认司机、车辆的上传状态
    public Result checkDriverAndVehicle(String shippingId) {

        HyShippingNote hyShippingNote = this.hyShippingNoteService.getById(shippingId);
        if (null == hyShippingNote) {
            return Result.error("运单不存在");
        }

        String driverId = hyShippingNote.getDriverId();
        String vehicleId = hyShippingNote.getVehicleId();
        if (StrUtil.isEmpty(driverId)) {
            return Result.error("当前运单司机信息不存在");
        }

        if (StrUtil.isEmpty(vehicleId)) {
            return Result.error("当前运单车辆信息不存在");
        }

        //自动上报司机和车辆配置
        String autoUpload = HyCfgUtil.getCfgItem2("mingna_report","auto_upload");

        boolean bDriverUploadStatus = this.hyDriverReportService.checkUploadStatus(driverId, hyShippingNote.getCompanyId(),
                ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode());
        if (!bDriverUploadStatus) {
            if (StrUtil.isEmpty(autoUpload) || !autoUpload.equals("1")) {
                return Result.error("请先同步司机数据");
            }

            log.warn("司机未上报铭钠监管，开始同步司机数据, {}", driverId);
            Result dResult = this.reportDriver(driverId, hyShippingNote.getCompanyId());
            if (!dResult.getCode().equals(CommonConstant.SC_OK_200)) {
                return dResult;
            }
        }

        boolean bVehicleUploadStatus = this.vehicleReportService.checkUploadStatus(vehicleId, hyShippingNote.getCompanyId(),
                ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode());
        if (!bVehicleUploadStatus) {
            if (StrUtil.isEmpty(autoUpload) || !autoUpload.equals("1")) {
                return Result.error("请先同步司机数据");
            }

            log.warn("车辆未上报铭钠监管，开始同步车辆数据, {}", vehicleId);
            Result vResult = this.reportVehicle(vehicleId, hyShippingNote.getCompanyId());
            if (!vResult.getCode().equals(CommonConstant.SC_OK_200)) {
                return vResult;
            }
        }

        return Result.OK();
    }


    public Result uploadCapitalFlow(String shippingId){

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("shipping_id", shippingId);
        List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.list(queryWrapper);

        if (CollectionUtils.isEmpty(shippingNoteBills)) {
            return Result.error("未找到对应数据");
        }

        HyShippingNote hyShippingNote = hyShippingNoteService.getById(shippingId);
        if (!hyShippingNote.getShippingPayStatus().equals(ShippingPayStatus.支付完成.getValue())) {
            return Result.error("该运单未支付完成");
        }

        //填入铭钠监管扩展字段值
        MingnaFinancialDTO mingnaFinancialDTO = new MingnaFinancialDTO();
        HyShippingNoteBill shippingNoteBillDb = shippingNoteBills.stream().filter(s->s.getBillType().equals("1")).findAny().orElse(null);
        mingnaFinancialDTO.setDocumentNumber(shippingNoteBillDb.getBillNum());
        mingnaFinancialDTO.setSendToProDateTime(DateUtils.dateToStringDateTime(new Date()));

        HyVehicle hyVehicle = this.vehicleService.getById(hyShippingNote.getVehicleId());
        if (null == hyVehicle) {
            return Result.error("承运车辆查找失败");
        }

        HyDriver hyDriver = this.driverService.getById(hyShippingNote.getDriverId());
        if (null == hyDriver) {
            return Result.error("承运司机查找失败");
        }

        if (hyVehicle.getOwner().contains("公司")) {
            mingnaFinancialDTO.setCarrier(hyVehicle.getOwner() + "(" + shippingNoteBillDb.getCarrier() + ")");
        } else {
            mingnaFinancialDTO.setCarrier(shippingNoteBillDb.getCarrier());
        }

        mingnaFinancialDTO.setActualCarrierId(shippingNoteBillDb.getActualCarrierId());
        mingnaFinancialDTO.setVehicleNumber(shippingNoteBillDb.getVehicleNumber());
        mingnaFinancialDTO.setVehiclePlateColorCode(shippingNoteBillDb.getVehicleColorCode());

        //运单列表
        List<ShippingNoteListDTO> shippingNoteListDTOS = new ArrayList <>();
        ShippingNoteListDTO shippingNoteListDTO = new ShippingNoteListDTO();
        shippingNoteListDTO.setShippingNoteNumber(shippingNoteBillDb.getShippingNoteNumber());
        shippingNoteListDTO.setSerialNumber("0000");
        BigDecimal costPayShipping = hyShippingNote.getDriverFee() ;
        shippingNoteListDTO.setTotalMonetaryAmount(costPayShipping.setScale(3, BigDecimal.ROUND_DOWN).toString());
        shippingNoteListDTOS.add(shippingNoteListDTO);
        mingnaFinancialDTO.setShippingNoteList(shippingNoteListDTOS);

        try {
            //财务列表
            List<FinanciallistDTO> financiallistDTOList = new ArrayList<>();
            for (HyShippingNoteBill shippingNoteBill: shippingNoteBills) {

                if (StrUtil.isEmpty(shippingNoteBill.getBankCode()) || StrUtil.isEmpty(CheckBankCardIUtil.getBankNameByBankCode(shippingNoteBill.getBankCode()))) {
                    return Result.error("资金流水单银行代码错误");
                }

                if (StrUtil.isEmpty(shippingNoteBill.getBankHdImg()) && StrUtil.isEmpty(shippingNoteBill.getBankHd())) {
                    return Result.error("银行支付凭证不能为空");
                }

                FinanciallistDTO financiallistDTO = new FinanciallistDTO();
                financiallistDTO.setShippingNoteNumber(shippingNoteBill.getShippingNoteNumber());
                financiallistDTO.setSerialNumber("0000");
                financiallistDTO.setDriverName(hyDriver.getDriverName());
                financiallistDTO.setDrivingLicense(hyDriver.getDrivingLicense());
                financiallistDTO.setPaymentMeansCode(PaymentMeansCodeEnum.PAYMENT_MEANS_TYPE_YHZZ.code);
                financiallistDTO.setReceiptAccount("1".equals(shippingNoteBill.getBillImportFlag()) ?
                                shippingNoteBill.getCraccno() :
                                shippingNoteBill.getCraccno() + shippingNoteBill.getCrlogaccno());
                financiallistDTO.setRecipient(shippingNoteBill.getCarrier());
                financiallistDTO.setBankCode(shippingNoteBill.getBankCode());
                financiallistDTO.setSequenceCode(shippingNoteBill.getBillNum());
                financiallistDTO.setMonetaryAmount(shippingNoteBill.getAmt().setScale(3, BigDecimal.ROUND_DOWN).toString());
                financiallistDTO.setDateTime(DateUtils.dateToStringDateTime(shippingNoteBill.getPayDt()));
                if (StrUtil.isNotEmpty(shippingNoteBill.getBankHdImg())) {
                    financiallistDTO.setPaymentImgs(this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator  + shippingNoteBill.getBankHdImg(), "银行支付凭证-image"));
                } else {
                    financiallistDTO.setPaymentImgs(this.mingnaReportPlatformService.uploadFile(bankpath + File.separator  + shippingNoteBill.getBankHd(), "银行支付凭证-pdf"));
                }
                financiallistDTOList.add(financiallistDTO);
            }
            mingnaFinancialDTO.setFinanciallist(financiallistDTOList);

            String jsonString = JSONObject.toJSONString(mingnaFinancialDTO);
            String reportDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_ZJ.getCode();
            ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reportDocType);
            String reportResult = this.mingnaReportPlatformService.sendReport(reportDocTypeEnum,jsonString);
            if (StringUtils.isNotEmpty(reportResult)) {
                R result = JSON.parseObject(reportResult, R.class);
                boolean res = result.getCode() == 0 ?  true : false;
                for(HyShippingNoteBill billdb : shippingNoteBills){
                    billdb.setUploadStatus(res ?  "1" : "2");
                    billdb.setUploadDate(new Date());
                    billdb.setReportFailureReason(res ?  "" : result.getMsg());
                    billdb.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode());
                }
                hyShippingNoteBillService.updateBatchById(shippingNoteBills);
                return res ? Result.OK("上报成功") : Result.error(result.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上传资金流水数据失败", e.getMessage());
        }

        return Result.error("上传资金流水数据失败");
    }

    public Result uploadShippingNote(HyShippingNote hyShippingNote) {
        HyShippingNote ShippingNoteDb = hyShippingNoteService.getById(hyShippingNote.getId());
        if (ShippingNoteDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }

        //超载判断
//        HyVehicle hyVehicle = this.vehicleService.getById(ShippingNoteDb.getVehicleId());
//        if (ShippingNoteDb.getGoodsWeightLoading().compareTo(hyVehicle.getVehicleTonnage()) > 0 ){
//            ShippingNoteDb.setUploadStatus("2");
//            ShippingNoteDb.setUploadDate(new Date());
//            ShippingNoteDb.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode());
//            ShippingNoteDb.setReportFailureReason("货物重量超过车辆核定载质量，货物重量: "+ShippingNoteDb.getGoodsWeightLoading()+"车辆核定载质量:"+hyVehicle.getVehicleTonnage());
//            this.hyShippingNoteService.updateById(ShippingNoteDb);
//            log.error("铭钠监管上报超载 货物重量 {} > 核定载质量 {}", ShippingNoteDb.getGoodsWeightLoading(), hyVehicle.getVehicleTonnage());
//            return Result.error("货物重量超过车辆核定载质量，货物重量:" + ShippingNoteDb.getGoodsWeightLoading() + " ,车辆核定载质量:" + hyVehicle.getVehicleTonnage());
//        }

        ShippingNoteDTO shippingNoteDTO = baseReportService.shippingNoteTOShippingNoteDTO(ShippingNoteDb, hyShippingNote.getCompanyId());

        MingnaShippingNoteDTO mingnaShippingNoteDTO = new MingnaShippingNoteDTO();
        BeanUtil.copyProperties(shippingNoteDTO, mingnaShippingNoteDTO, true);

        String reportDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_YD.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reportDocType);

        try {

            mingnaShippingNoteDTO.setContractFile(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator  + hyShippingNote.getShippingContract(), "司机承运人合同")
            );

            //装卸货照片
            List<String> loadImages = new ArrayList<>() ;
            if (StrUtil.isNotEmpty(hyShippingNote.getLoadingSceneImg())) {
                for (int i = 0; i < hyShippingNote.getLoadingSceneImg().split(",").length; i++) {
                    String imgLoading = this.mingnaReportPlatformService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getLoadingSceneImg().split(",")[i], "装货现场照片");
                    loadImages.add(imgLoading);
                }
            }
            mingnaShippingNoteDTO.setLoadImgs(loadImages.stream().collect(Collectors.joining(",")));

            List<String> unloadImages = new ArrayList<>() ;
            if (StrUtil.isNotEmpty(hyShippingNote.getUnloadingSceneImg())) {
                for (int i = 0; i < hyShippingNote.getUnloadingSceneImg().split(",").length; i++) {
                    String imgUnLoading = this.mingnaReportPlatformService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getUnloadingSceneImg().split(",")[i], "卸货现场照片");
                    unloadImages.add(imgUnLoading);
                }
            }
            mingnaShippingNoteDTO.setUnloadImgs(unloadImages.stream().collect(Collectors.joining(",")));

            //磅单照片必填
            List<String> backDocumentImages = new ArrayList<>() ;
            if (StrUtil.isNotEmpty(hyShippingNote.getImgLoading())) {
                for (int i = 0; i < hyShippingNote.getImgLoading().split(",").length; i++) {
                    String imgLoading = this.mingnaReportPlatformService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getImgLoading().split(",")[i], "(回单)装货凭证照片");
                    backDocumentImages.add(imgLoading);
                }
            }

            if (StrUtil.isNotEmpty(hyShippingNote.getImgUnloading())) {
                for (int i = 0; i < hyShippingNote.getImgUnloading().split(",").length; i++) {
                    String imgUnLoading = this.mingnaReportPlatformService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getImgUnloading().split(",")[i], "(回单)卸货凭证照片");
                    backDocumentImages.add(imgUnLoading);
                }
            }
            mingnaShippingNoteDTO.setDocumentImgs(backDocumentImages.stream().collect(Collectors.joining(",")));

            String jsonString = JSONObject.toJSONString(mingnaShippingNoteDTO);

            String reportResult = this.mingnaReportPlatformService.sendReport(reportDocTypeEnum, jsonString);
            if (StringUtils.isNotEmpty(reportResult)) {
                R result = JSON.parseObject(reportResult, R.class);
                boolean res = result.getCode() == 0  ? true : false;
                ShippingNoteDb.setUploadStatus(res ? "1" : "2");
                ShippingNoteDb.setUploadDate(new Date());
                ShippingNoteDb.setReportFailureReason(res ? "" : result.getMsg());
                ShippingNoteDb.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode());
                hyShippingNoteService.updateById(ShippingNoteDb);
                return res ? Result.OK("上报成功") : Result.error(result.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上传运单数据失败", e.getMessage());
        }

        return Result.error("上传运单数据失败");
    }

    /**
     * 平台司机上报
     *
     * @param driverId
     * @return
     */
    @Override
    public Result reportDriver(String driverId, String companyId) {
        HyDriver hyDriverDb = driverService.getById(driverId);
        if (hyDriverDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }

        HyDriverReport hyDriverReport = new HyDriverReport();
        DriverDTO driverDTO = baseReportService.hyDriverTODriverDTO(hyDriverDb);
        //添加图片等扩展字段
        MingnaDriverDTO mingnaDriverDTO = new MingnaDriverDTO();
        BeanUtil.copyProperties(driverDTO, mingnaDriverDTO);

        String reportDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_JSY.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reportDocType);

        //上传图片
        try {
            mingnaDriverDTO.setIdcardFirstImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyDriverDb.getIdcardImg(), "司机 " + hyDriverDb.getDriverName() + "(身份证人像面图片)")
            );

            mingnaDriverDTO.setIdcardSecondImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyDriverDb.getIdcardImgBack(), "司机 " + hyDriverDb.getDriverName() + "(身份证国徽面图片)")
            );

            mingnaDriverDTO.setDrivingLicenseFirstImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyDriverDb.getDriverImg(), "司机 " + hyDriverDb.getDriverName() + "(驾驶证主页照片)")
            );

            mingnaDriverDTO.setDrivingLicenseSecondImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyDriverDb.getDriverImg2(), "司机 " + hyDriverDb.getDriverName() + "(驾驶证副页照片)")
            );

            mingnaDriverDTO.setQualificationCertificateImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyDriverDb.getQualificationImg(), "司机 " + hyDriverDb.getDriverName() + "(从业资格证照片)")
            );

            String jsonString = JSONObject.toJSONString(mingnaDriverDTO);
            String reportResult = this.mingnaReportPlatformService.sendReport(reportDocTypeEnum, jsonString);
            if (StringUtils.isNotEmpty(reportResult)) {
                R result = JSON.parseObject(reportResult, R.class);
                boolean res = result.getCode() == 0 ? true : false;
                hyDriverReport.setDriverId(driverId);
                hyDriverReport.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode());
                hyDriverReport.setDriverUploadStatus(res ? "1" : "2");
                hyDriverReport.setUploadDate(new Date());
                hyDriverReport.setCompanyId(companyId);
                hyDriverReport.setDriverReportFailureReason(result.getMsg());
                hyDriverReportService.saveDriverReport(hyDriverReport);

                boolean bUpdate = this.driverService.updateJgUploadStatusById(driverId, "1");
                log.info("铭钠监管司机上报成功, {}, {}", bUpdate, driverId);
                return res ? Result.OK("上报成功") : Result.error(result.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上传司机数据失败", e.getMessage());
        }

        return Result.error("上传司机数据失败");
    }

    public Result reportVehicle(String vehicleId, String companyId) {

        HyVehicle hyVehicleDb = this.vehicleService.getById(vehicleId);
        HyVehicleReport hyVehicleReport = new HyVehicleReport();
        HyVehicleType hyVehicleType = vehicleService.getHyVehicleType(hyVehicleDb.getVehicleType());
        if (hyVehicleType == null) {
            throw new JeecgBootException("车辆类型不存在,修改后请重新上报！");
        }

        String reportDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_CL.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reportDocType);

        hyVehicleDb.setVehicleType(hyVehicleType.getValue());
        VehicleDTO vehicleDTO = baseReportService.hyVehicleDTOTOVehicleDTO(hyVehicleDb);

        MingnaVehicleDTO mingnaVehicleDTO = new MingnaVehicleDTO();
        BeanUtil.copyProperties(vehicleDTO, mingnaVehicleDTO);

        try {
            mingnaVehicleDTO.setVehicleLicenseFirstImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyVehicleDb.getDrivingLicenseImg(), "车辆 " + hyVehicleDb.getVehicleNumber() + "(行驶证主页照片)")
            );
            mingnaVehicleDTO.setVehicleLicenseSecondImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyVehicleDb.getDrivingLicenseImg2(), "车辆 " + hyVehicleDb.getVehicleNumber() + "(行驶证副页照片)")
            );
            mingnaVehicleDTO.setRoadTransportCertificateFirstImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyVehicleDb.getRoadTransportImg(), "车辆 " + hyVehicleDb.getVehicleNumber() + "(道路运输证照片)")
            );
            mingnaVehicleDTO.setRoadTransportCertificateSecondImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyVehicleDb.getRoadTransportImg2(), "车辆 " + hyVehicleDb.getVehicleNumber() + "(道路运输证照片)")
            );
            mingnaVehicleDTO.setVehicleDriverImg(
                    this.mingnaReportPlatformService.uploadFile(uploadpath + File.separator + hyVehicleDb.getVehicleImg(), "车辆 " + hyVehicleDb.getVehicleNumber() + "(司机照片)")
            );

            String jsonString = JSONObject.toJSONString(mingnaVehicleDTO);
            String reportResult = this.mingnaReportPlatformService.sendReport(reportDocTypeEnum, jsonString);
            if(StringUtils.isNotEmpty(reportResult)){
                R result = JSON.parseObject(reportResult, R.class);
                boolean res =  result.getCode() == 0 ?  true : false;

                hyVehicleReport.setVehicleId(vehicleId);
                hyVehicleReport.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode());
                hyVehicleReport.setVehicleUploadStatus(res ? "1" : "2");
                hyVehicleReport.setUploadDate( new Date() );
                hyVehicleReport.setCompanyId(companyId);
                hyVehicleReport.setVehicleReportFailureReason(result.getMsg());
                vehicleReportService.saveVehicleReport(hyVehicleReport);

                boolean bUpdate = this.vehicleService.updateJgUploadStatusById(vehicleId, "1");
                log.info("铭钠监管车辆上报成功, {}, {}", bUpdate, vehicleId);
                return res ? Result.OK("上报成功") : Result.error(result.getMsg());
            }
        } catch (Exception e){
            e.printStackTrace();
            log.error("上传车辆数据失败", e.getMessage());
        }

        return Result.error("上传车辆数据失败");
    }


}
